<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>商品上传</title>
</head>
<body>
<%--<form action="${pageContext.servletContext.contextPath}" method="post">--%>
    <p>商品名称</p>
    <input type="text" name="comname" value=""/><br>
    <p>商品描述</p>
    <textarea name="miaoshu" style="height: 60px"></textarea><br>
    <p>商品类型</p>
    <select name="lx">
        <c:forEach var="malltype" items="${malltypes}">
            <option value="${malltype.pbid}">${malltype.pbname}</option>
        </c:forEach>
    </select>
    <br>
    <span>商品规格</span>
    <span class="add" style="color:blue">添加规格</span><p></p>
    <br>
    <button>提交</button>
<%--    </form>--%>
<span id="err" style="text-align: center"></span>
</body>
<script type="text/javascript" src="${pageContext.servletContext.contextPath}/st/front/JS/jquery-3.2.1.min.js"></script>
<script>
    $(function(){
        $(".add").click(function(){
            $("p:last").after(
                "<span>价格</span>"+
                "<input type='text' name='price' />"+
                "<span>配置</span>"+
                "<input type='text' name='config' />"+
                "<span>库存</span>"+
                "<input type='text' name='count'/>"+
                "<span>图片</span>"+
                "<input type='file' name='files' /><br>");

        });
        $("button").click(function(){
            var price=$("[name=price]").val();
            var config=$("[name=config]").val();
            var count=$("[name=count]").val();
            var files=$("[name=files]").val();
            if(name!=null&&config!=null&&count!=null&&files!=null){
                //获取上传的文件
                var formData = new FormData();
                formData.append("files", $("[name='files']")[0].files[0]);
                formData.append("comname",$("[name='comname']").val());
                formData.append("miaoshu",$("[name='miaoshu']").val());
                formData.append("lx",$("[name='lx']").val());
                formData.append("price",price);
                formData.append("config",config);
                formData.append("count",count);
                formData.append("files",files);
                $.ajax({
                    url:'${pageContext.servletContext.contextPath}/UpFile',
                    dataType:'json',
                    type:'POST',
                    data: formData,
                    processData : false, // 使数据不做处理
                    contentType : false, // 不要设置Content-Type请求头
                    success: function(result){
                        if(result*1==1) {
                            $("#err").css("color","green") ;
                            $("#err").html("上传成功") ;
                        }
                        else {
                            $("#err").html(result.err) ;
                            //清空upFile
                            $(upFile).val("");
                            $(upFile).text("");
                        }
                    },
                    error:function(response){
                        console.log(response);
                    }
                })
                // $("form").submit();
            }else{
                alert("填写不完整！");
                return false;
            }
        })
    })
</script>
</html>

